#include "dungeon.h"


dungeon::dungeon()
{
	playerx = 0;
	playery = 0;
	srand(time(NULL));
	for (int i = 0; i < 10; i++){
		for (int j = 0; j < 10; j++){
			map[j][i] = '.';
		}
	}
}


dungeon::~dungeon()
{
}

void dungeon::draw(){
	system("cls");
	map[playerx][playery] = 'P';
	for (int i = 0; i < 10; i++){
		for (int j = 0; j < 10; j++){
			cout << map[j][i];
		}
		cout << endl;
	}
}

void dungeon::setup(){
	int x, y;

	//place 3-6 random traps
	int traps = rand() % 6 + 3; 
	for (int i = 0; i < traps; i++){
		x = rand() % 10;
		y = rand() % 10;
		map[x][y] = 'T';
	}

	//place treasure
	do{
		x = rand() % 10;
		y = rand() % 10;
	} while (map[x][y] == 'T');
	map[x][y] = 'X';

	//place player
	do{
		x = rand() % 10;
		y = rand() % 10;
	} while (map[x][y] == 'T' || map[x][y] == 'X');
	playerx = x;
	playery = y;
}

void dungeon::play(){
	int playing = 1;
	int ch;
	while (playing){
		ch = _getch();
		switch (ch){
		case KEY_UP:
			if (playery > 0){
				map[playerx][playery] = '.';
				playery -= 1;
				playing = 0;
			}
			break;
		case KEY_DOWN:
			if (playery < 9){
				map[playerx][playery] = '.';
				playery += 1;
				playing = 0;
			}
			break;
		case KEY_LEFT:
			if (playerx > 0){
				map[playerx][playery] = '.';
				playerx -= 1;
				playing = 0;
			}
			break;
		case KEY_RIGHT:
			if (playerx < 9){
				map[playerx][playery] = '.';
				playerx += 1;
				playing = 0;
			}
			break;
		default:
			break;
		}
	}
}

int dungeon::getState(){
	if (map[playerx][playery] == 'T'){
		cout << "LOSE"<< endl;
		return 1;
	}
	else if (map[playerx][playery] == 'X'){
		cout << "WIN"<< endl;
		return 2;
	}
	else{
		return 0;
	}
}